package linkedList;

import listinterface.IList;
import myexception.ListIndexOutOfBoundsException;
import singleList.MySingleList;

public class MyLinkedList implements IList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

    @Override
    public void addIndex(int index, int data) {
        //先绑定后面的
        //修改每个指向的时候 ， 不要修改了前面的而影响了后面的
        int len = size();
        if (index < 0 || index > len) {
            throw new ListIndexOutOfBoundsException("双向链表 index 位置不合法" +index);
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == len) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        node.next = cur;
//        cur.prev = node;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }
    private ListNode findIndex(int index) {
        ListNode cur = head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //考虑只删除一个节点的情况
        ListNode cur = head;
        while(cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        last = null;
                        return;
                    }
                    head.prev = null;
                } else{
                    if (cur == last) {
                        last = last.prev;
                        last.next = null;
                    } else {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            } else {
                cur = cur.next;
            }

        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        last = null;
                        return;
                    }
                    head.prev = null;
                } else{
                    if (cur == last) {
                        last = last.prev;
                        last.next = null;
                    } else {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
            } else {
                cur = cur.next;
            }

        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;

    }

    @Override
//    public void clear(ListNode head) {
//        ListNode cur = head;
//        MySingleList.ListNode curN = cur.next;
//        while(cur != cur) {
//            //对于 int 类型来说 ，val 可以不置为空 ， 但是引用类型不许置空
////            cur.val = null;
//            cur.next = null;
//            cur.prev = null;
//            cur = curN;
//            curN = cur.next;
//        }
//        head = null;
//        last = null;
//    }
    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode curN = cur.next;
//            cur.val = null;
            cur.next = null;
            cur.prev = null;
            cur = curN;
        }
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.println(cur.val +" ");
            cur = cur.next;
        }
        System.out.println();

    }

    @Override
    public MySingleList.ListNode reverse() {
        return null;
    }


}
